'use strict';
const db = uniCloud.database()
const coll = db.collection('all')
const dbCmd = db.command
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	let suijishu = Math.floor(Math.random() * (10 - 1)) + 1;
	if (event.sign == 1) {//查询人数
		let res = await coll.get()
		return res
	} else if(event.sign == 2){//点亮自增
		const coll1 = db.collection('user')
		const res0 = await coll1.where({
			openid:event.openid
		}).get()
		if (res0.data[0].status == 0) {
			//点亮人数自增 并且记录第几名点亮的到自己数据记录
			let res = await coll.where({
				_id: '28ee4e3e609f62a518d6800e57ec5fb6'
			}).update({
				num: dbCmd.inc(1)
			})
			let res1 = await coll.where({
				_id: '28ee4e3e609f62a518d6800e57ec5fb6'
			}).get()
			let res2 = await coll1.where({
				openid:event.openid
			}).update({
				num:res1.data[0].num,
				status:1,
				suijishu:suijishu
			})
			// console.log(res2)
			return {success:true,msg:'您已成功点亮心灵之旅',num:res1.data[0].num,nickname:res0.data[0].nickname,suijishu:suijishu}
			
		} else{
			return {success:false,msg:'您已经点亮过心灵之旅',num:res0.data[0].num,nickname:res0.data[0].nickname,suijishu:res0.data[0].suijishu}
		}
	
		
		
		
		
		
	}else if(event.sign == 3){//修改总参与人数
		let res = await coll.where({
		  _id: '28ee4e3e609f67e718d88a412559c770'
		}).update({
		  num: Number(event.all)
		})
		let res1 = await coll.where({
		  _id: '28ee4e3e609f67e718d88a412559c770'
		}).get()
		return res1
	}else{
		//重置点亮人数 归零
		let res = await coll.where({
		  _id: '28ee4e3e609f62a518d6800e57ec5fb6'
		}).update({
		  num: 0
		})
		let res1 = await coll.limit(1).get()
		return res1
	}
	
	
	//返回数据给客户端
	
};
